In [1]:
##BIBLIOTECAS
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose
import yfinance as yf
from binance.client import Client
1) Aquisição dos dados de ações¶
In [2]:
ticket = yf.Ticker("BTC-USD")
#df = ticket.history(period='3y', interval='1mo')
df = ticket.history(interval='1d', start='2018-01-01', end='2024-12-31')
In [3]:
df
Out[3]:
| Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2018-01-01 00:00:00+00:00 | 14112.200195 | 14112.200195 | 13154.700195 | 13657.200195 | 10291200000 | 0.0 | 0.0 |
| 2018-01-02 00:00:00+00:00 | 13625.000000 | 15444.599609 | 13163.599609 | 14982.099609 | 16846600192 | 0.0 | 0.0 |
| 2018-01-03 00:00:00+00:00 | 14978.200195 | 15572.799805 | 14844.500000 | 15201.000000 | 16871900160 | 0.0 | 0.0 |
| 2018-01-04 00:00:00+00:00 | 15270.700195 | 15739.700195 | 14522.200195 | 15599.200195 | 21783199744 | 0.0 | 0.0 |
| 2018-01-05 00:00:00+00:00 | 15477.200195 | 17705.199219 | 15202.799805 | 17429.500000 | 23840899072 | 0.0 | 0.0 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2024-12-26 00:00:00+00:00 | 99297.695312 | 99884.570312 | 95137.882812 | 95795.515625 | 47054980873 | 0.0 | 0.0 |
| 2024-12-27 00:00:00+00:00 | 95704.976562 | 97294.843750 | 93310.742188 | 94164.859375 | 52419934565 | 0.0 | 0.0 |
| 2024-12-28 00:00:00+00:00 | 94160.187500 | 95525.898438 | 94014.289062 | 95163.929688 | 24107436185 | 0.0 | 0.0 |
| 2024-12-29 00:00:00+00:00 | 95174.054688 | 95174.875000 | 92881.789062 | 93530.226562 | 29635885267 | 0.0 | 0.0 |
| 2024-12-30 00:00:00+00:00 | 93527.195312 | 94903.320312 | 91317.132812 | 92643.210938 | 56188003691 | 0.0 | 0.0 |
2556 rows × 7 columns
In [4]:
df.tail(15)
Out[4]:
| Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2024-12-16 00:00:00+00:00 | 104293.578125 | 107780.578125 | 103322.984375 | 106029.718750 | 91020417816 | 0.0 | 0.0 |
| 2024-12-17 00:00:00+00:00 | 106030.687500 | 108268.445312 | 105291.734375 | 106140.601562 | 68589364868 | 0.0 | 0.0 |
| 2024-12-18 00:00:00+00:00 | 106147.296875 | 106470.609375 | 100041.539062 | 100041.539062 | 93865656139 | 0.0 | 0.0 |
| 2024-12-19 00:00:00+00:00 | 100070.687500 | 102748.148438 | 95587.679688 | 97490.953125 | 97221662392 | 0.0 | 0.0 |
| 2024-12-20 00:00:00+00:00 | 97484.695312 | 98098.914062 | 92175.179688 | 97755.929688 | 105634083408 | 0.0 | 0.0 |
| 2024-12-21 00:00:00+00:00 | 97756.195312 | 99507.101562 | 96426.523438 | 97224.726562 | 51765334294 | 0.0 | 0.0 |
| 2024-12-22 00:00:00+00:00 | 97218.320312 | 97360.265625 | 94202.187500 | 95104.937500 | 43147981314 | 0.0 | 0.0 |
| 2024-12-23 00:00:00+00:00 | 95099.390625 | 96416.210938 | 92403.132812 | 94686.242188 | 65239002919 | 0.0 | 0.0 |
| 2024-12-24 00:00:00+00:00 | 94684.343750 | 99404.062500 | 93448.015625 | 98676.093750 | 47114953674 | 0.0 | 0.0 |
| 2024-12-25 00:00:00+00:00 | 98675.914062 | 99478.750000 | 97593.468750 | 99299.195312 | 33700394629 | 0.0 | 0.0 |
| 2024-12-26 00:00:00+00:00 | 99297.695312 | 99884.570312 | 95137.882812 | 95795.515625 | 47054980873 | 0.0 | 0.0 |
| 2024-12-27 00:00:00+00:00 | 95704.976562 | 97294.843750 | 93310.742188 | 94164.859375 | 52419934565 | 0.0 | 0.0 |
| 2024-12-28 00:00:00+00:00 | 94160.187500 | 95525.898438 | 94014.289062 | 95163.929688 | 24107436185 | 0.0 | 0.0 |
| 2024-12-29 00:00:00+00:00 | 95174.054688 | 95174.875000 | 92881.789062 | 93530.226562 | 29635885267 | 0.0 | 0.0 |
| 2024-12-30 00:00:00+00:00 | 93527.195312 | 94903.320312 | 91317.132812 | 92643.210938 | 56188003691 | 0.0 | 0.0 |
In [5]:
df[['Close']].head()
Out[5]:
| Close | |
|---|---|
| Date | |
| 2018-01-01 00:00:00+00:00 | 13657.200195 |
| 2018-01-02 00:00:00+00:00 | 14982.099609 |
| 2018-01-03 00:00:00+00:00 | 15201.000000 |
| 2018-01-04 00:00:00+00:00 | 15599.200195 |
| 2018-01-05 00:00:00+00:00 | 17429.500000 |
In [6]:
decomposicao = seasonal_decompose(df[['Close']], model='additive', period=18, extrapolate_trend=30)
In [7]:
df[['Close']].plot()
Out[7]:
<Axes: xlabel='Date'>
In [8]:
df['Close'].head()
Out[8]:
Date 2018-01-01 00:00:00+00:00 13657.200195 2018-01-02 00:00:00+00:00 14982.099609 2018-01-03 00:00:00+00:00 15201.000000 2018-01-04 00:00:00+00:00 15599.200195 2018-01-05 00:00:00+00:00 17429.500000 Name: Close, dtype: float64
In [9]:
decomposicao.seasonal + decomposicao.resid + decomposicao.trend
Out[9]:
Date
2018-01-01 00:00:00+00:00 13657.200195
2018-01-02 00:00:00+00:00 14982.099609
2018-01-03 00:00:00+00:00 15201.000000
2018-01-04 00:00:00+00:00 15599.200195
2018-01-05 00:00:00+00:00 17429.500000
...
2024-12-26 00:00:00+00:00 95795.515625
2024-12-27 00:00:00+00:00 94164.859375
2024-12-28 00:00:00+00:00 95163.929688
2024-12-29 00:00:00+00:00 93530.226562
2024-12-30 00:00:00+00:00 92643.210938
Length: 2556, dtype: float64
In [10]:
decomposicao.trend.iloc[0:5]
Out[10]:
Date 2018-01-01 00:00:00+00:00 15880.219946 2018-01-02 00:00:00+00:00 15684.101005 2018-01-03 00:00:00+00:00 15487.982064 2018-01-04 00:00:00+00:00 15291.863124 2018-01-05 00:00:00+00:00 15095.744183 Name: trend, dtype: float64
In [11]:
decomposicao.plot();
In [12]:
decomposicao_multi = seasonal_decompose(df[['Close']], model='multiplicative', period=18, extrapolate_trend=30)
In [13]:
decomposicao.seasonal
Out[13]:
Date
2018-01-01 00:00:00+00:00 -10.311643
2018-01-02 00:00:00+00:00 -54.266927
2018-01-03 00:00:00+00:00 72.603848
2018-01-04 00:00:00+00:00 108.665000
2018-01-05 00:00:00+00:00 230.689072
...
2024-12-26 00:00:00+00:00 -37.067414
2024-12-27 00:00:00+00:00 -114.561807
2024-12-28 00:00:00+00:00 -58.202163
2024-12-29 00:00:00+00:00 -89.561017
2024-12-30 00:00:00+00:00 -31.124074
Name: seasonal, Length: 2556, dtype: float64
In [14]:
decomposicao_multi.seasonal
Out[14]:
Date
2018-01-01 00:00:00+00:00 0.999088
2018-01-02 00:00:00+00:00 0.999050
2018-01-03 00:00:00+00:00 1.002395
2018-01-04 00:00:00+00:00 1.002652
2018-01-05 00:00:00+00:00 1.004031
...
2024-12-26 00:00:00+00:00 0.997920
2024-12-27 00:00:00+00:00 0.996226
2024-12-28 00:00:00+00:00 0.996047
2024-12-29 00:00:00+00:00 0.994488
2024-12-30 00:00:00+00:00 0.996755
Name: seasonal, Length: 2556, dtype: float64
In [15]:
max(decomposicao_multi.resid)
Out[15]:
1.1935827593006558
In [16]:
ax, fig = plt.subplots(figsize=(15,8))
plt.plot(decomposicao.observed)
plt.plot(decomposicao.trend)
Out[16]:
[<matplotlib.lines.Line2D at 0x14e9e932ed0>]
In [17]:
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4,1, figsize=(12,8))
decomposicao.observed.plot(ax=ax1)
decomposicao.trend.plot(ax=ax2)
decomposicao.seasonal.plot(ax=ax3)
decomposicao.resid.plot(ax=ax4)
plt.tight_layout()
3) Cálculo da média móvel¶
In [18]:
df ['Close']
Out[18]:
Date
2018-01-01 00:00:00+00:00 13657.200195
2018-01-02 00:00:00+00:00 14982.099609
2018-01-03 00:00:00+00:00 15201.000000
2018-01-04 00:00:00+00:00 15599.200195
2018-01-05 00:00:00+00:00 17429.500000
...
2024-12-26 00:00:00+00:00 95795.515625
2024-12-27 00:00:00+00:00 94164.859375
2024-12-28 00:00:00+00:00 95163.929688
2024-12-29 00:00:00+00:00 93530.226562
2024-12-30 00:00:00+00:00 92643.210938
Name: Close, Length: 2556, dtype: float64
In [19]:
df['Close'].rolling(7).mean()
Out[19]:
Date
2018-01-01 00:00:00+00:00 NaN
2018-01-02 00:00:00+00:00 NaN
2018-01-03 00:00:00+00:00 NaN
2018-01-04 00:00:00+00:00 NaN
2018-01-05 00:00:00+00:00 NaN
...
2024-12-26 00:00:00+00:00 96934.662946
2024-12-27 00:00:00+00:00 96421.652902
2024-12-28 00:00:00+00:00 96127.253348
2024-12-29 00:00:00+00:00 95902.294643
2024-12-30 00:00:00+00:00 95610.433036
Name: Close, Length: 2556, dtype: float64
In [20]:
media_movel7d = df['Close'].rolling(7).mean()
media_movel14d = df['Close'].rolling(14).mean()
media_movel21d = df['Close'].rolling(21).mean()
In [21]:
fig, ax = plt.subplots(figsize=(14,5))
plt.plot(media_movel7d, 'orange')
plt.plot(media_movel14d, 'red')
plt.plot(media_movel21d, 'black')
plt.plot(df['Close'])
Out[21]:
[<matplotlib.lines.Line2D at 0x14e9ffcb190>]
4) Extração de features¶
In [22]:
df.head()
Out[22]:
| Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2018-01-01 00:00:00+00:00 | 14112.200195 | 14112.200195 | 13154.700195 | 13657.200195 | 10291200000 | 0.0 | 0.0 |
| 2018-01-02 00:00:00+00:00 | 13625.000000 | 15444.599609 | 13163.599609 | 14982.099609 | 16846600192 | 0.0 | 0.0 |
| 2018-01-03 00:00:00+00:00 | 14978.200195 | 15572.799805 | 14844.500000 | 15201.000000 | 16871900160 | 0.0 | 0.0 |
| 2018-01-04 00:00:00+00:00 | 15270.700195 | 15739.700195 | 14522.200195 | 15599.200195 | 21783199744 | 0.0 | 0.0 |
| 2018-01-05 00:00:00+00:00 | 15477.200195 | 17705.199219 | 15202.799805 | 17429.500000 | 23840899072 | 0.0 | 0.0 |
In [23]:
df.reset_index(inplace=True)
In [24]:
df.head()
Out[24]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2018-01-01 00:00:00+00:00 | 14112.200195 | 14112.200195 | 13154.700195 | 13657.200195 | 10291200000 | 0.0 | 0.0 |
| 1 | 2018-01-02 00:00:00+00:00 | 13625.000000 | 15444.599609 | 13163.599609 | 14982.099609 | 16846600192 | 0.0 | 0.0 |
| 2 | 2018-01-03 00:00:00+00:00 | 14978.200195 | 15572.799805 | 14844.500000 | 15201.000000 | 16871900160 | 0.0 | 0.0 |
| 3 | 2018-01-04 00:00:00+00:00 | 15270.700195 | 15739.700195 | 14522.200195 | 15599.200195 | 21783199744 | 0.0 | 0.0 |
| 4 | 2018-01-05 00:00:00+00:00 | 15477.200195 | 17705.199219 | 15202.799805 | 17429.500000 | 23840899072 | 0.0 | 0.0 |
In [25]:
# criar features para cada período
df['year'] = df['Date'].dt.year
df['month'] = df['Date'].dt.month
df['day'] = df['Date'].dt.day
In [26]:
df.head()
Out[26]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | year | month | day | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2018-01-01 00:00:00+00:00 | 14112.200195 | 14112.200195 | 13154.700195 | 13657.200195 | 10291200000 | 0.0 | 0.0 | 2018 | 1 | 1 |
| 1 | 2018-01-02 00:00:00+00:00 | 13625.000000 | 15444.599609 | 13163.599609 | 14982.099609 | 16846600192 | 0.0 | 0.0 | 2018 | 1 | 2 |
| 2 | 2018-01-03 00:00:00+00:00 | 14978.200195 | 15572.799805 | 14844.500000 | 15201.000000 | 16871900160 | 0.0 | 0.0 | 2018 | 1 | 3 |
| 3 | 2018-01-04 00:00:00+00:00 | 15270.700195 | 15739.700195 | 14522.200195 | 15599.200195 | 21783199744 | 0.0 | 0.0 | 2018 | 1 | 4 |
| 4 | 2018-01-05 00:00:00+00:00 | 15477.200195 | 17705.199219 | 15202.799805 | 17429.500000 | 23840899072 | 0.0 | 0.0 | 2018 | 1 | 5 |
In [27]:
# Cálculo da rentabilidade: preço atual/ preço anterior * 100 - 100
df[['Close']]
Out[27]:
| Close | |
|---|---|
| 0 | 13657.200195 |
| 1 | 14982.099609 |
| 2 | 15201.000000 |
| 3 | 15599.200195 |
| 4 | 17429.500000 |
| ... | ... |
| 2551 | 95795.515625 |
| 2552 | 94164.859375 |
| 2553 | 95163.929688 |
| 2554 | 93530.226562 |
| 2555 | 92643.210938 |
2556 rows × 1 columns
In [28]:
df['Close'].head()
Out[28]:
0 13657.200195 1 14982.099609 2 15201.000000 3 15599.200195 4 17429.500000 Name: Close, dtype: float64
In [29]:
df['Close'].shift()
Out[29]:
0 NaN
1 13657.200195
2 14982.099609
3 15201.000000
4 15599.200195
...
2551 99299.195312
2552 95795.515625
2553 94164.859375
2554 95163.929688
2555 93530.226562
Name: Close, Length: 2556, dtype: float64
In [30]:
df['rentabilidade'] = df['Close'] / df['Close'].shift() * 100 - 100
In [31]:
df.head()
Out[31]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | year | month | day | rentabilidade | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2018-01-01 00:00:00+00:00 | 14112.200195 | 14112.200195 | 13154.700195 | 13657.200195 | 10291200000 | 0.0 | 0.0 | 2018 | 1 | 1 | NaN |
| 1 | 2018-01-02 00:00:00+00:00 | 13625.000000 | 15444.599609 | 13163.599609 | 14982.099609 | 16846600192 | 0.0 | 0.0 | 2018 | 1 | 2 | 9.701106 |
| 2 | 2018-01-03 00:00:00+00:00 | 14978.200195 | 15572.799805 | 14844.500000 | 15201.000000 | 16871900160 | 0.0 | 0.0 | 2018 | 1 | 3 | 1.461080 |
| 3 | 2018-01-04 00:00:00+00:00 | 15270.700195 | 15739.700195 | 14522.200195 | 15599.200195 | 21783199744 | 0.0 | 0.0 | 2018 | 1 | 4 | 2.619566 |
| 4 | 2018-01-05 00:00:00+00:00 | 15477.200195 | 17705.199219 | 15202.799805 | 17429.500000 | 23840899072 | 0.0 | 0.0 | 2018 | 1 | 5 | 11.733293 |
In [32]:
def features_extraction(df_):
#criar features para cada período de forma diferente
df_['year'] = df_['Date'].dt.year
df_['month'] = df_['Date'].dt.month
df_['day'] = df_['Date'].dt.day
df_['rentabilidade'] = df_['Close'] / df_['Close'].shift() * 100 - 100
In [33]:
df.reset_index(inplace=True)
In [34]:
features_extraction(df)
In [35]:
df.head()
Out[35]:
| index | Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | year | month | day | rentabilidade | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 2018-01-01 00:00:00+00:00 | 14112.200195 | 14112.200195 | 13154.700195 | 13657.200195 | 10291200000 | 0.0 | 0.0 | 2018 | 1 | 1 | NaN |
| 1 | 1 | 2018-01-02 00:00:00+00:00 | 13625.000000 | 15444.599609 | 13163.599609 | 14982.099609 | 16846600192 | 0.0 | 0.0 | 2018 | 1 | 2 | 9.701106 |
| 2 | 2 | 2018-01-03 00:00:00+00:00 | 14978.200195 | 15572.799805 | 14844.500000 | 15201.000000 | 16871900160 | 0.0 | 0.0 | 2018 | 1 | 3 | 1.461080 |
| 3 | 3 | 2018-01-04 00:00:00+00:00 | 15270.700195 | 15739.700195 | 14522.200195 | 15599.200195 | 21783199744 | 0.0 | 0.0 | 2018 | 1 | 4 | 2.619566 |
| 4 | 4 | 2018-01-05 00:00:00+00:00 | 15477.200195 | 17705.199219 | 15202.799805 | 17429.500000 | 23840899072 | 0.0 | 0.0 | 2018 | 1 | 5 | 11.733293 |
Hipotese: 1) Existe um melhor mês para investir no ativo?¶
Verificar o mês com menor rentabilidade, ou seja, período de baixa (nos últimos 7 anos):¶
In [36]:
df.groupby('month').agg({'rentabilidade':'sum'}).plot(kind='bar')
Out[36]:
<Axes: xlabel='month'>
In [37]:
df.set_index('Date', inplace=True)
In [38]:
media_movel30d = df['Close'].rolling(30).mean()
media_movel90d = df['Close'].rolling(90).mean()
fig, ax = plt.subplots(figsize=(8,4))
plt.plot(df['Close'])
plt.plot(media_movel30d, 'orange')
plt.plot(media_movel90d, 'green')
Out[38]:
[<matplotlib.lines.Line2D at 0x14e9e7c7d10>]
Hipotese: 2) Existe um melhor dia para investir no BTC?¶
In [39]:
df.groupby('day').agg({'rentabilidade':'sum'}).plot(kind='bar')
Out[39]:
<Axes: xlabel='day'>
OBS: Quanto menor a sua rentabilidade, melhor oportunidade de compra.¶
In [40]:
df.iloc[0]
Out[40]:
index 0.000000e+00 Open 1.411220e+04 High 1.411220e+04 Low 1.315470e+04 Close 1.365720e+04 Volume 1.029120e+10 Dividends 0.000000e+00 Stock Splits 0.000000e+00 year 2.018000e+03 month 1.000000e+00 day 1.000000e+00 rentabilidade NaN Name: 2018-01-01 00:00:00+00:00, dtype: float64
5) Correlação de séries temporais¶
In [41]:
tickets = ['ETH-USD', 'SOL-USD', 'BNB-USD', 'XRP-USD', 'ADA-USD', 'USDBRL=X']
In [42]:
dfs = []
for t in tickets:
print('Reading ticker {}...' .format(t))
ticket = yf.Ticker(t)
aux = ticket.history(interval='1d', start='2021-01-01', end='2024-12-31')
aux.reset_index(inplace=True)
aux['ticket'] = t
dfs.append(aux)
Reading ticker ETH-USD... Reading ticker SOL-USD... Reading ticker BNB-USD... Reading ticker XRP-USD... Reading ticker ADA-USD... Reading ticker USDBRL=X...
In [43]:
dfs[1]
Out[43]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | ticket | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 2021-01-01 00:00:00+00:00 | 1.509775 | 1.859656 | 1.502038 | 1.842084 | 25722549 | 0.0 | 0.0 | SOL-USD |
| 1 | 2021-01-02 00:00:00+00:00 | 1.845586 | 1.989295 | 1.721482 | 1.799275 | 31671064 | 0.0 | 0.0 | SOL-USD |
| 2 | 2021-01-03 00:00:00+00:00 | 1.799902 | 2.364981 | 1.799902 | 2.161752 | 55073422 | 0.0 | 0.0 | SOL-USD |
| 3 | 2021-01-04 00:00:00+00:00 | 2.162412 | 2.485097 | 1.876342 | 2.485097 | 59955405 | 0.0 | 0.0 | SOL-USD |
| 4 | 2021-01-05 00:00:00+00:00 | 2.490982 | 2.502616 | 2.077742 | 2.157217 | 50555207 | 0.0 | 0.0 | SOL-USD |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1455 | 2024-12-26 00:00:00+00:00 | 197.475159 | 199.397980 | 186.855591 | 188.218109 | 2864701219 | 0.0 | 0.0 | SOL-USD |
| 1456 | 2024-12-27 00:00:00+00:00 | 188.216553 | 193.753357 | 182.947128 | 183.828476 | 2788285851 | 0.0 | 0.0 | SOL-USD |
| 1457 | 2024-12-28 00:00:00+00:00 | 183.833389 | 195.435440 | 183.833328 | 195.013535 | 2158687217 | 0.0 | 0.0 | SOL-USD |
| 1458 | 2024-12-29 00:00:00+00:00 | 195.022446 | 197.181076 | 188.341003 | 189.744263 | 2165753102 | 0.0 | 0.0 | SOL-USD |
| 1459 | 2024-12-30 00:00:00+00:00 | 189.744202 | 196.122910 | 185.937607 | 191.028046 | 3246300054 | 0.0 | 0.0 | SOL-USD |
1460 rows × 9 columns
In [44]:
for d in dfs:
features_extraction(d)
In [45]:
dfs[0]
Out[45]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | ticket | year | month | day | rentabilidade | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2021-01-01 00:00:00+00:00 | 737.708374 | 749.201843 | 719.792236 | 730.367554 | 13652004358 | 0.0 | 0.0 | ETH-USD | 2021 | 1 | 1 | NaN |
| 1 | 2021-01-02 00:00:00+00:00 | 730.402649 | 786.798462 | 718.109497 | 774.534973 | 19740771179 | 0.0 | 0.0 | ETH-USD | 2021 | 1 | 2 | 6.047287 |
| 2 | 2021-01-03 00:00:00+00:00 | 774.511841 | 1006.565002 | 771.561646 | 975.507690 | 45200463368 | 0.0 | 0.0 | ETH-USD | 2021 | 1 | 3 | 25.947533 |
| 3 | 2021-01-04 00:00:00+00:00 | 977.058838 | 1153.189209 | 912.305359 | 1040.233032 | 56945985763 | 0.0 | 0.0 | ETH-USD | 2021 | 1 | 4 | 6.635042 |
| 4 | 2021-01-05 00:00:00+00:00 | 1041.498779 | 1129.371460 | 986.811279 | 1100.006104 | 41535932781 | 0.0 | 0.0 | ETH-USD | 2021 | 1 | 5 | 5.746123 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1455 | 2024-12-26 00:00:00+00:00 | 3493.304199 | 3512.604492 | 3302.306396 | 3331.225830 | 22247726776 | 0.0 | 0.0 | ETH-USD | 2024 | 12 | 26 | -4.637807 |
| 1456 | 2024-12-27 00:00:00+00:00 | 3331.053711 | 3436.710693 | 3302.575684 | 3328.916992 | 24091627403 | 0.0 | 0.0 | ETH-USD | 2024 | 12 | 27 | -0.069309 |
| 1457 | 2024-12-28 00:00:00+00:00 | 3328.774658 | 3419.920166 | 3318.033936 | 3397.902344 | 14305648523 | 0.0 | 0.0 | ETH-USD | 2024 | 12 | 28 | 2.072306 |
| 1458 | 2024-12-29 00:00:00+00:00 | 3397.862549 | 3406.648438 | 3321.664795 | 3349.513428 | 13440907792 | 0.0 | 0.0 | ETH-USD | 2024 | 12 | 29 | -1.424082 |
| 1459 | 2024-12-30 00:00:00+00:00 | 3349.585938 | 3428.527344 | 3298.804443 | 3356.392578 | 26981583962 | 0.0 | 0.0 | ETH-USD | 2024 | 12 | 30 | 0.205378 |
1460 rows × 13 columns
In [46]:
correlacao = pd.DataFrame()
for d in dfs:
correlacao[d['ticket'].iloc[0]] = d['rentabilidade']
In [47]:
correlacao.head()
Out[47]:
| ETH-USD | SOL-USD | BNB-USD | XRP-USD | ADA-USD | USDBRL=X | |
|---|---|---|---|---|---|---|
| 0 | NaN | NaN | NaN | NaN | NaN | NaN |
| 1 | 6.047287 | -2.323944 | 0.887962 | -6.649569 | 1.182210 | -0.003857 |
| 2 | 25.947533 | 20.145722 | 7.602682 | 1.891682 | 15.540266 | 1.991568 |
| 3 | 6.635042 | 14.957541 | -0.541024 | 4.794818 | 9.642667 | -0.211507 |
| 4 | 5.746123 | -13.193848 | 1.974881 | -4.194326 | 14.927800 | 0.545032 |
In [48]:
correlacao.corr()
Out[48]:
| ETH-USD | SOL-USD | BNB-USD | XRP-USD | ADA-USD | USDBRL=X | |
|---|---|---|---|---|---|---|
| ETH-USD | 1.000000 | 0.625088 | 0.639170 | 0.543584 | 0.673762 | -0.015829 |
| SOL-USD | 0.625088 | 1.000000 | 0.546888 | 0.442368 | 0.545256 | -0.028577 |
| BNB-USD | 0.639170 | 0.546888 | 1.000000 | 0.469745 | 0.550990 | -0.002799 |
| XRP-USD | 0.543584 | 0.442368 | 0.469745 | 1.000000 | 0.568396 | 0.019685 |
| ADA-USD | 0.673762 | 0.545256 | 0.550990 | 0.568396 | 1.000000 | 0.000785 |
| USDBRL=X | -0.015829 | -0.028577 | -0.002799 | 0.019685 | 0.000785 | 1.000000 |
OBS: Quanto mais próximos de 1, mais correlacionados são os ativos.¶
Visualização de dados usando Seaborn¶
In [49]:
import seaborn as sns
In [50]:
ax, fig = plt.subplots(figsize=(20,5))
ax = sns.heatmap(correlacao.corr(), annot=True)
Visualização de dados usando Plotly¶
In [51]:
import plotly.graph_objs as go
import plotly.io as pio
pio.renderers.default = 'notebook'
In [52]:
def plot_lines(df_, columns=['Open', 'Close', 'High', 'Low']):
fig = go.Figure()
for c in columns:
fig.add_trace(go.Scatter(x = list(df_.index),
y = df_[c],
mode = 'markers+lines',
name = c))
fig.show()
In [53]:
plot_lines(df)
In [54]:
def plotCandleStick(df, acao='tickets'):
tracel = {
'x': df.index,
'open': df.Open,
'close': df.Close,
'high': df.High,
'low': df.Low,
'type': 'candlestick',
'name': acao,
'showlegend': False
}
data = [tracel]
layout = go.Layout()
fig = go.Figure(data=data, layout=layout)
fig.show()
In [55]:
plotCandleStick(df)
In [ ]: